package com.zmn.oms.business.impl.report;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.manager.mq.interfaces.ZmnMQSender;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.common.constant.OrderExportConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.utils.MoneyUtil;
import com.zmn.oms.constant.MessageQueueTopicConstant;
import com.zmn.oms.model.entity.report.EsReportPayQuery;
import com.zmn.oms.model.vo.report.ReportPayDetailVO;
import com.zmn.oms.model.vo.report.ReportPayVO;
import com.zmn.oms.business.interfaces.report.ReportPayBService;
import com.zmn.oms.services.interfaces.report.ReportPayService;
import com.zmn.pay.common.constant.PayConsts;
import com.zmn.tapi.common.export.model.dio.AddExportTaskDIO;
import com.zmn.tapi.common.export.model.dio.ExportHttpDIO;
import com.zmn.tapi.common.export.model.dio.SheetDIO;
import com.zmn.tapi.dubbo.interfaces.export.ExportModifyRemoteService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author ：wangxiaokun
 * @date ：2021/5/10 17:24
 * @description：支付明细报表
 */
@Service
public class ReportPayBServiceImpl implements ReportPayBService {

    private Logger logger = LoggerFactory.getLogger(ReportPayBServiceImpl.class);

    // 导出类型
    private static final String EXPORT_ORDER_PAY_LIST_TYPE = "订单收款汇总";
    private static final String EXPORT_ORDER_PAY_REFUND_LIST_TYPE = "订单退款汇总";
    private static final String EXPORT_ORDER_PAY_DETAIL_LIST_TYPE = "订单收付明细";

    // 导出文件类型
    private static final String EXPORT_FILE_TYPE = ".xlsx";

    // sheet1名称
    private static final String ORDER_SHEET_ONE_NAME = "服务分类汇总";

    // sheet2名称
    private static final String ORDER_SHEET_TWO_NAME = "公司汇总";

    // sheet3名称
    private static final String ORDER_SHEET_THREE_NAME = "支付明细";

    private static final int sheetOneSize = 17;

    @Resource
    private ReportPayService reportPayService;
    @Reference(version = com.zmn.tapi.dubbo.DubboConsts.INTERFACE_VERSION, check = false)
    private ExportModifyRemoteService exportModifyRemoteService;
    @Resource
    ZmnMQSender zmnMQSender;

    /**
     * 订单收款汇总
     * @param query
     * @return
     */
    @Override
    public List<ReportPayVO> listAmountSummaryByQuery(EsReportPayQuery query) {

        // 查询列表
        List<ReportPayVO> reportVOS = reportPayService.listGroupByCompanyFromPay(query);

        if (CollectionUtils.isEmpty(reportVOS)) {
            return reportVOS;
        }

        reportVOS.sort((a, b) -> ((int) (b.getTotalAmount() - a.getTotalAmount())));// 应收降序
        // 总计对象
        ReportPayVO sumObjVO = reportVOS.stream().reduce(new ReportPayVO(), (sumObj, obj) -> {
            sumObj.setRealityTotalAmount((sumObj.getRealityTotalAmount() != null ? sumObj.getRealityTotalAmount() : 0) + Optional.ofNullable(obj.getRealityTotalAmount()).orElse(0L));
            sumObj.setTotalAmount((sumObj.getTotalAmount() != null ? sumObj.getTotalAmount() : 0) + Optional.ofNullable(obj.getTotalAmount()).orElse(0L));
            sumObj.setChannelpayAmount((sumObj.getChannelpayAmount() != null ? sumObj.getChannelpayAmount() : 0) + Optional.ofNullable(obj.getChannelpayAmount()).orElse(0L));
            sumObj.setAlipayAmount((sumObj.getAlipayAmount() != null ? sumObj.getAlipayAmount() : 0) + Optional.ofNullable(obj.getAlipayAmount()).orElse(0L));
            sumObj.setWeixinpayAmount((sumObj.getWeixinpayAmount() != null ? sumObj.getWeixinpayAmount() : 0) + Optional.ofNullable(obj.getWeixinpayAmount()).orElse(0L));
            sumObj.setIcbcpayAmount((sumObj.getIcbcpayAmount() != null ? sumObj.getIcbcpayAmount() : 0) + Optional.ofNullable(obj.getIcbcpayAmount()).orElse(0L));
            sumObj.setChinaumsQRAmount((sumObj.getChinaumsQRAmount() != null ? sumObj.getChinaumsQRAmount() : 0) + Optional.ofNullable(obj.getChinaumsQRAmount()).orElse(0L));
            sumObj.setChinaumsAlipayAppAmount((sumObj.getChinaumsAlipayAppAmount() != null ? sumObj.getChinaumsAlipayAppAmount() : 0) + Optional.ofNullable(obj.getChinaumsAlipayAppAmount()).orElse(0L));
            sumObj.setChinaumsWechatpayAppAmount((sumObj.getChinaumsWechatpayAppAmount() != null ? sumObj.getChinaumsWechatpayAppAmount() : 0) + Optional.ofNullable(obj.getChinaumsWechatpayAppAmount()).orElse(0L));
            sumObj.setBankpayAmount((sumObj.getBankpayAmount() != null ? sumObj.getBankpayAmount() : 0) + Optional.ofNullable(obj.getBankpayAmount()).orElse(0L));
            sumObj.setYipayAmount((sumObj.getYipayAmount() != null ? sumObj.getYipayAmount() : 0) + Optional.ofNullable(obj.getYipayAmount()).orElse(0L));
            sumObj.setCashAmount((sumObj.getCashAmount() != null ? sumObj.getCashAmount() : 0) + Optional.ofNullable(obj.getCashAmount()).orElse(0L));
            sumObj.setBalancepayAmount((sumObj.getBalancepayAmount() != null ? sumObj.getBalancepayAmount() : 0) + Optional.ofNullable(obj.getBalancepayAmount()).orElse(0L));
            sumObj.setEciticAmount((sumObj.getEciticAmount() != null ? sumObj.getEciticAmount() : 0) + Optional.ofNullable(obj.getEciticAmount()).orElse(0L));
            sumObj.setBaidupayAmount((sumObj.getBaidupayAmount() != null ? sumObj.getBaidupayAmount() : 0) + Optional.ofNullable(obj.getBaidupayAmount()).orElse(0L));
            sumObj.setKuaishoupayAmount((sumObj.getKuaishoupayAmount() != null ? sumObj.getKuaishoupayAmount() : 0) + Optional.ofNullable(obj.getKuaishoupayAmount()).orElse(0L));
            sumObj.setDuplicateAmount((sumObj.getDuplicateAmount() != null ? sumObj.getDuplicateAmount() : 0) + Optional.ofNullable(obj.getDuplicateAmount()).orElse(0L));
            sumObj.setWarrantyAmount((sumObj.getWarrantyAmount() != null ? sumObj.getWarrantyAmount() : 0) + Optional.ofNullable(obj.getWarrantyAmount()).orElse(0L));

            return sumObj;
        });
        sumObjVO.setServCategName("汇总");
        sumObjVO.setCompanyName("汇总");
        ArrayList<ReportPayVO> allList = new ArrayList<>(reportVOS.size() + 1);
        allList.add(sumObjVO);
        allList.addAll(reportVOS);
        return allList;
    }

    /**
     * 订单退款汇总
     * @param query
     * @return
     */
    @Override
    public List<ReportPayVO> listRefundAmountSummaryByQuery(EsReportPayQuery query) {

        // 查询列表
        List<ReportPayVO> reportVOS = reportPayService.listRefundGroupByCompanyFromPay(query);

        if (CollectionUtils.isEmpty(reportVOS)) {
            return reportVOS;
        }

        reportVOS.sort((a, b) -> ((int) (b.getTotalAmount() - a.getTotalAmount())));// 应收降序
        // 总计对象
        ReportPayVO sumObjVO = reportVOS.stream().reduce(new ReportPayVO(), (sumObj, obj) -> {
            sumObj.setRealityTotalAmount((sumObj.getRealityTotalAmount() != null ? sumObj.getRealityTotalAmount() : 0) + Optional.ofNullable(obj.getRealityTotalAmount()).orElse(0L));
            sumObj.setTotalAmount((sumObj.getTotalAmount() != null ? sumObj.getTotalAmount() : 0) + Optional.ofNullable(obj.getTotalAmount()).orElse(0L));
            sumObj.setChannelpayAmount((sumObj.getChannelpayAmount() != null ? sumObj.getChannelpayAmount() : 0) + Optional.ofNullable(obj.getChannelpayAmount()).orElse(0L));
            sumObj.setAlipayAmount((sumObj.getAlipayAmount() != null ? sumObj.getAlipayAmount() : 0) + Optional.ofNullable(obj.getAlipayAmount()).orElse(0L));
            sumObj.setWeixinpayAmount((sumObj.getWeixinpayAmount() != null ? sumObj.getWeixinpayAmount() : 0) + Optional.ofNullable(obj.getWeixinpayAmount()).orElse(0L));
            sumObj.setIcbcpayAmount((sumObj.getIcbcpayAmount() != null ? sumObj.getIcbcpayAmount() : 0) + Optional.ofNullable(obj.getIcbcpayAmount()).orElse(0L));
            sumObj.setChinaumsQRAmount((sumObj.getChinaumsQRAmount() != null ? sumObj.getChinaumsQRAmount() : 0) + Optional.ofNullable(obj.getChinaumsQRAmount()).orElse(0L));
            sumObj.setChinaumsAlipayAppAmount((sumObj.getChinaumsAlipayAppAmount() != null ? sumObj.getChinaumsAlipayAppAmount() : 0) + Optional.ofNullable(obj.getChinaumsAlipayAppAmount()).orElse(0L));
            sumObj.setChinaumsWechatpayAppAmount((sumObj.getChinaumsWechatpayAppAmount() != null ? sumObj.getChinaumsWechatpayAppAmount() : 0) + Optional.ofNullable(obj.getChinaumsWechatpayAppAmount()).orElse(0L));
            sumObj.setBankpayAmount((sumObj.getBankpayAmount() != null ? sumObj.getBankpayAmount() : 0) + Optional.ofNullable(obj.getBankpayAmount()).orElse(0L));
            sumObj.setYipayAmount((sumObj.getYipayAmount() != null ? sumObj.getYipayAmount() : 0) + Optional.ofNullable(obj.getYipayAmount()).orElse(0L));
            sumObj.setCashAmount((sumObj.getCashAmount() != null ? sumObj.getCashAmount() : 0) + Optional.ofNullable(obj.getCashAmount()).orElse(0L));
            sumObj.setBalancepayAmount((sumObj.getBalancepayAmount() != null ? sumObj.getBalancepayAmount() : 0) + Optional.ofNullable(obj.getBalancepayAmount()).orElse(0L));
            sumObj.setEciticAmount((sumObj.getEciticAmount() != null ? sumObj.getEciticAmount() : 0) + Optional.ofNullable(obj.getEciticAmount()).orElse(0L));
            sumObj.setBaidupayAmount((sumObj.getBaidupayAmount() != null ? sumObj.getBaidupayAmount() : 0) + Optional.ofNullable(obj.getBaidupayAmount()).orElse(0L));
            sumObj.setKuaishoupayAmount((sumObj.getKuaishoupayAmount() != null ? sumObj.getKuaishoupayAmount() : 0) + Optional.ofNullable(obj.getKuaishoupayAmount()).orElse(0L));
            return sumObj;
        });
        sumObjVO.setServCategName("汇总");
        sumObjVO.setCompanyName("汇总");
        ArrayList<ReportPayVO> allList = new ArrayList<>(reportVOS.size() + 1);
        allList.add(sumObjVO);
        allList.addAll(reportVOS);
        return allList;
    }

    @Override
    public List<ReportPayDetailVO> listPayDetailByQuery(EsReportPayQuery query) {
        List<ReportPayDetailVO> list = reportPayService.listDetailByQuery(query);
        return list;
    }

    /**
     * 创建导出任务
     * @param query
     */
    @Override
    public void buildExportTask(EsReportPayQuery query) throws OmsBaseException {
        StringBuilder fileName = new StringBuilder();
        StringBuilder exportType = new StringBuilder();
        switch (query.getPlat()) {
            case GlobalConsts.PLAT_MARK_ZMN:
                fileName.append("啄");
                break;
            case GlobalConsts.PLAT_MARK_YEYX:
                fileName.append("言");
                break;
            case GlobalConsts.PLAT_MARK_CNHB:
                fileName.append("川");
                break;
            default:
                throw new OmsBaseException("不支持导出");
        }
        fileName.append("-");
        switch (query.getExportType()) {
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST:
                fileName.append(EXPORT_ORDER_PAY_LIST_TYPE);
                exportType.append(EXPORT_ORDER_PAY_LIST_TYPE);
                break;
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST:
                fileName.append(EXPORT_ORDER_PAY_REFUND_LIST_TYPE);
                exportType.append(EXPORT_ORDER_PAY_REFUND_LIST_TYPE);
                break;
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_DETAIL_LIST:
                fileName.append(EXPORT_ORDER_PAY_DETAIL_LIST_TYPE);
                exportType.append(EXPORT_ORDER_PAY_DETAIL_LIST_TYPE);
                break;
            default:
                throw new OmsBaseException("不支持导出");
        }

        AddExportTaskDIO addExportTaskDIO = AddExportTaskDIO.builder()
                .exportType(exportType.toString())
                .exportName(fileName.toString())
                .fileType(EXPORT_FILE_TYPE)
                .staffId(query.getStaffId())
                .creater(query.getOperator())
                .build();
        ResponseDTO<Integer> responseDTO = exportModifyRemoteService.addExportTask(addExportTaskDIO);
        if (!responseDTO.isSuccess() || Objects.isNull(responseDTO.getData())) {
            throw new OmsBaseException("导出失败");
        }

        Integer exportTaskId = responseDTO.getData();
        query.setExportTaskId(exportTaskId);
        String json = JSON.toJSONString(query);
        String key = String.format("%s-%s", StringUtil.getRandomStringAndNumber3(26), exportTaskId);
        logger.debug("异步导出工单列表key：【{}】", key);
        zmnMQSender.send(MessageQueueTopicConstant.ZMN_TOPIC_OMS, MessageQueueTagConsts.ORDER_PAY_ASYNC_EXPORT, key, json);
    }

    /**
     * 异步导出
     * @param query
     */
    @Override
    public void asyncExportPayListByQuery(EsReportPayQuery query) {
        ResponseDTO<String> exportToken = exportModifyRemoteService.getExportToken(query.getExportTaskId());
        if (!exportToken.isSuccess() || Objects.isNull(exportToken.getData())) {
            logger.info("异步导出获取Token失败，taskId={}", query.getExportTaskId());
            return;
        }
        query.setExportToken(exportToken.getData());
        switch (query.getExportType()) {
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST:
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST:
                this.orderPayExport(query);
                break;
            case OrderExportConsts.EXPORT_TYPE_ORDER_PAY_DETAIL_LIST:
                this.orderDetailPayExport(query);
                break;
            default:
                break;
        }
    }

    /**
     * 收付汇总导出
     * @param query
     */
    private void orderPayExport(EsReportPayQuery query) {

        List<SheetDIO> sheetDIOList = Lists.newArrayListWithCapacity(3);
        SheetDIO sheetOne = SheetDIO.builder()
                .sheetName(ORDER_SHEET_ONE_NAME)
                .build();
        SheetDIO sheetTwo = SheetDIO.builder()
                .sheetName(ORDER_SHEET_TWO_NAME)
                .build();
        SheetDIO sheetThree = SheetDIO.builder()
                .sheetName(ORDER_SHEET_THREE_NAME)
                .build();

        // 服务分类汇总sheet
        query.setGroupFiled("servCategId");
        List<ReportPayVO> servCategList = null;
        if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST)) {
            servCategList = this.listRefundAmountSummaryByQuery(query);
        } else {
            servCategList = this.listAmountSummaryByQuery(query);
        }
        List<List<String>> sheetOneList = Lists.newArrayListWithCapacity(servCategList.size() + 1);
        sheetOneList.add(this.getFirstRow(query, GlobalConsts.NO));

        servCategList.forEach(report -> {
            List<String> sheetOneRowList = Lists.newArrayListWithCapacity(sheetOneSize);
            sheetOneRowList.add(report.getServCategName());
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getRealityTotalAmount()));
            }
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getTotalAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChannelpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getAlipayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getWeixinpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getEciticAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getBankpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getIcbcpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChinaumsAlipayAppAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChinaumsQRAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChinaumsWechatpayAppAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getBaidupayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getKuaishoupayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getYipayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getWarrantyAmount()));
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getCashAmount()));
            }
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getBalancepayAmount()));
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getCashAmount()));
            }
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getDuplicateAmount()));
            }
            sheetOneList.add(sheetOneRowList);
        });
        sheetOne.setExportContents(sheetOneList);

        // 公司汇总sheet
        query.setGroupFiled("companyId");
        List<ReportPayVO> companyList = null;
        if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST)) {
            companyList = this.listRefundAmountSummaryByQuery(query);
        } else {
            companyList = this.listAmountSummaryByQuery(query);
        }
        List<List<String>> sheetTwoList = Lists.newArrayListWithCapacity(companyList.size() + 1);
        sheetTwoList.add(this.getFirstRow(query, GlobalConsts.YES));

        companyList.forEach(report -> {
            List<String> sheetOneRowList = Lists.newArrayListWithCapacity(sheetOneSize);
            sheetOneRowList.add(report.getCompanyName());
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getRealityTotalAmount()));
            }
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getTotalAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChannelpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getAlipayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getWeixinpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getEciticAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getBankpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getIcbcpayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChinaumsAlipayAppAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChinaumsQRAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getChinaumsWechatpayAppAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getBaidupayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getKuaishoupayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getYipayAmount()));
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getWarrantyAmount()));
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getCashAmount()));
            }
            sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getBalancepayAmount()));
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getCashAmount()));
            }
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneRowList.add(MoneyUtil.longParseToYuanStr(report.getDuplicateAmount()));
            }
            sheetTwoList.add(sheetOneRowList);
        });
        sheetTwo.setExportContents(sheetTwoList);

        // 支付明细sheet
        query.setPayStatusList(Lists.newArrayList(PayConsts.REFUND_STATUS_DONE, PayConsts.PAY_STATUS_DONE));
        this.getDetailPaySheet(query, sheetThree);

        sheetDIOList.add(sheetOne);
        sheetDIOList.add(sheetTwo);
        sheetDIOList.add(sheetThree);

        // 请求导出
        this.exportHttps(query, sheetDIOList);
    }

    /**
     * 获取支付明细sheet
     * @param query
     * @return
     */
    private void getDetailPaySheet(EsReportPayQuery query, SheetDIO sheetThree) {
        query.setPageSize(10000);
        List<ReportPayDetailVO> detailPayList = this.listPayDetailByQuery(query);
        List<List<String>> sheetThreeList = Lists.newArrayListWithCapacity(detailPayList.size() + 1);
        query.setExportType(OrderExportConsts.EXPORT_TYPE_ORDER_PAY_DETAIL_LIST);
        sheetThreeList.add(this.getFirstRow(query, GlobalConsts.YES));

        detailPayList.forEach(detail -> {
            List<String> sheetOneRowList = Lists.newArrayListWithCapacity(sheetOneSize);
            sheetOneRowList.add(detail.getCompanyName());
            sheetOneRowList.add(detail.getCityName());
            sheetOneRowList.add(detail.getServCategName());
            sheetOneRowList.add(String.valueOf(detail.getWorkId()));
            sheetOneRowList.add(detail.getTradeTypeName());
            sheetOneRowList.add(detail.getPayChannelName());
            sheetOneRowList.add(detail.getPayTime() == null ? "" : DateUtil.dateFormatToString(detail.getPayTime(), DateUtil.FORMAT_DEFAULT));
            sheetOneRowList.add(detail.getInnerTradeNo());
            sheetOneRowList.add(detail.getPayOutTradeNo());
            sheetOneRowList.add(detail.getAmount() == null ? "0" : com.zmn.common.utils.math.MoneyUtil.parseToYuanStr(detail.getAmount().intValue()));

            // 退款信息
            if (Objects.equals(detail.getRefundType(), GlobalConsts.NONE)) {
                sheetOneRowList.add(detail.getAmountTypeName());
                sheetOneRowList.add("");
                sheetOneRowList.add("");
                sheetOneRowList.add("");
                sheetOneRowList.add("");
            } else {
                sheetOneRowList.add("");
                sheetOneRowList.add(detail.getRefundType() == GlobalConsts.NO ? "原路返回" : "线下退款");
                sheetOneRowList.add(detail.getPayChannelName());
                sheetOneRowList.add(detail.getPayeeThirdAccount());
                sheetOneRowList.add(detail.getOrderCreater());
            }
            sheetOneRowList.add(detail.getProductInfo());
            sheetOneRowList.add(detail.getDuplicate() == GlobalConsts.NO ? "否" : "是");

            sheetThreeList.add(sheetOneRowList);
        });
        sheetThree.setExportContents(sheetThreeList);
    }

    /**
     * 获取表格第一行
     * @param query
     * @param type
     * @return
     */
    private List<String> getFirstRow(EsReportPayQuery query, Integer type) {
        if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_DETAIL_LIST)) {
            List<String> sheetOneFirstRowList = Lists.newArrayListWithCapacity(sheetOneSize);
            sheetOneFirstRowList.add("公司");
            sheetOneFirstRowList.add("城市");
            sheetOneFirstRowList.add("服务分类");
            sheetOneFirstRowList.add("工单号");
            sheetOneFirstRowList.add("交易类型");
            sheetOneFirstRowList.add("支付渠道");
            sheetOneFirstRowList.add("支付时间");
            sheetOneFirstRowList.add("商家流水号");
            sheetOneFirstRowList.add("支付流水号");
            sheetOneFirstRowList.add("金额");
            sheetOneFirstRowList.add("类型");
            sheetOneFirstRowList.add("退款方式");
            sheetOneFirstRowList.add("退款路径");
            sheetOneFirstRowList.add("退款账户");
            sheetOneFirstRowList.add("完成人");
            sheetOneFirstRowList.add("产品名称");
            sheetOneFirstRowList.add("是否重复支付");
            return sheetOneFirstRowList;
        } else {
            List<String> sheetOneFirstRowList = Lists.newArrayListWithCapacity(sheetOneSize);

            if (Objects.equals(type, GlobalConsts.NO)) {
                sheetOneFirstRowList.add("服务分类");
            } else {
                sheetOneFirstRowList.add("分公司");
            }
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneFirstRowList.add("实收总计");
                sheetOneFirstRowList.add("收款总计");
            } else {
                sheetOneFirstRowList.add("退款总计");
            }
            sheetOneFirstRowList.add("渠道预收");
            sheetOneFirstRowList.add("支付宝");
            sheetOneFirstRowList.add("微信");
            sheetOneFirstRowList.add("银联-中信");
            sheetOneFirstRowList.add("银行转账");
            sheetOneFirstRowList.add("工行");
            sheetOneFirstRowList.add("银商-支付宝");
            sheetOneFirstRowList.add("银商-H5");
            sheetOneFirstRowList.add("银商-微信");
            sheetOneFirstRowList.add("百度支付");
            sheetOneFirstRowList.add("快手支付");
            sheetOneFirstRowList.add("翼支付");
            sheetOneFirstRowList.add("质保金支付");
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneFirstRowList.add("退款总计");
            }
            sheetOneFirstRowList.add("虚拟账户");
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_REFUND_LIST)) {
                sheetOneFirstRowList.add("线下支付");
            }
            if (Objects.equals(query.getExportType(), OrderExportConsts.EXPORT_TYPE_ORDER_PAY_LIST)) {
                sheetOneFirstRowList.add("重复支付");
            }

            return sheetOneFirstRowList;
        }
    }

    /**
     * 支付明细导出
     * @param query
     */
    private void orderDetailPayExport(EsReportPayQuery query) {
        SheetDIO sheetOne = SheetDIO.builder().sheetName(ORDER_SHEET_THREE_NAME).build();

        // 组装支付明细数据
        this.getDetailPaySheet(query, sheetOne);

        // 请求导出
        this.exportHttps(query, Lists.newArrayList(sheetOne));
    }

    /**
     * http请求tapi导出数据任务
     * @param query
     * @param list
     */
    private void exportHttps(EsReportPayQuery query, List<SheetDIO> list) {
        ExportHttpDIO exportHttpDIO = new ExportHttpDIO();
        exportHttpDIO.setExportTaskId(query.getExportTaskId());
        exportHttpDIO.setToken(query.getExportToken());
        exportHttpDIO.setList(Lists.newArrayList(list));
        OrderExportConsts.exportHttps(exportHttpDIO);
    }
}
